// pages/index/index.js
const { getSubjectInfo, DEFAULT_SUBJECT } = require('../../utils/subjectConfig.js');

Page({
  data: {
    currentSubject: DEFAULT_SUBJECT,
    subjectName: '英语',
    pageTitle: '深圳1年级英语练习'
  },

  onLoad() {
    this.loadSubject();
  },

  onShow() {
    // 每次显示页面时重新加载学科
    this.loadSubject();
  },

  /**
   * 加载科目信息
   * 从本地存储读取当前科目，并使用统一配置获取科目信息
   */
  loadSubject() {
    const subject = wx.getStorageSync('currentSubject') || DEFAULT_SUBJECT;
    const subjectInfo = getSubjectInfo(subject);
    
    this.setData({
      currentSubject: subject,
      subjectName: subjectInfo.name,
      pageTitle: subjectInfo.title
    });
  },

  goToSubject() {
    wx.navigateTo({
      url: '/pages/subject/subject'
    });
  },

  // 英语模块
  goToLetters() {
    wx.navigateTo({
      url: '/pages/letters/letters'
    });
  },

  goToWords() {
    wx.navigateTo({
      url: '/pages/words/words'
    });
  },

  goToSpelling() {
    wx.navigateTo({
      url: '/pages/spelling/spelling'
    });
  },

  goToListening() {
    wx.navigateTo({
      url: '/pages/listening/listening'
    });
  },

  goToChallenge() {
    wx.navigateTo({
      url: '/pages/challenge/challenge'
    });
  },

  // 语文模块
  goToPinyin() {
    wx.navigateTo({
      url: '/pages/chinese/pinyin/pinyin'
    });
  },

  goToStrokes() {
    wx.navigateTo({
      url: '/pages/chinese/strokes/strokes'
    });
  },

  goToChineseChallenge() {
    wx.navigateTo({
      url: '/pages/chinese/challenge/challenge'
    });
  },

  // 数学模块
  goToMathGrade(e) {
    const grade = e.currentTarget.dataset.grade;
    wx.navigateTo({
      url: `/pages/math/math?grade=${grade}`
    });
  }
});

